import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import 'font-awesome/css/font-awesome.css';
import axios from 'axios';
import qs from 'qs';
import {
    getCurDate,
    setSessionStorage,
    getSessionStorage,
    removeSessionStorage,
    setLocalStorage,
    getLocalStorage,
    removeLocalStorage
} from './common.js';
// 导入统一的API配置
import api, { userApi, businessApi, foodApi, cartApi, orderApi, addressApi } from './api/index.js';
import { getBaseURL } from './api/config.js';

// 设置axios的baseURL
axios.defaults.baseURL = getBaseURL();

// 添加请求拦截器
axios.interceptors.request.use(
    config => {
        console.log('发送请求:', config.method.toUpperCase(), config.url);
        return config;
    },
    error => {
        console.error('请求错误:', error);
        return Promise.reject(error);
    }
);

// 添加响应拦截器
axios.interceptors.response.use(
    response => {
        console.log('收到响应:', response.status, response.config.url);
        return response;
    },
    error => {
        console.error('响应错误:', error.response?.status, error.config?.url);
        if (error.response?.status === 500) {
            alert('服务器内部错误，请稍后重试');
        } else if (error.response?.status === 404) {
            alert('请求的资源不存在');
        }
        return Promise.reject(error);
    }
);
const app = createApp(App);

// 提供axios实例给整个应用（支持Composition API）
app.provide('axios', axios);

// 提供统一的API模块给整个应用
app.provide('api', api);
app.provide('userApi', userApi);
app.provide('businessApi', businessApi);
app.provide('foodApi', foodApi);
app.provide('cartApi', cartApi);
app.provide('orderApi', orderApi);
app.provide('addressApi', addressApi);

// 全局属性配置（支持Options API）
app.config.globalProperties.$api = api;
app.config.globalProperties.$userApi = userApi;
app.config.globalProperties.$businessApi = businessApi;
app.config.globalProperties.$foodApi = foodApi;
app.config.globalProperties.$cartApi = cartApi;
app.config.globalProperties.$orderApi = orderApi;
app.config.globalProperties.$addressApi = addressApi;
app.provide('qs', qs);

// 保持向后兼容（Options API）
app.config.globalProperties.$axios = axios;
app.config.globalProperties.$qs = qs;
app.config.globalProperties.$getCurDate = getCurDate;
app.config.globalProperties.$setSessionStorage = setSessionStorage;
app.config.globalProperties.$getSessionStorage = getSessionStorage;
app.config.globalProperties.$removeSessionStorage = removeSessionStorage;
app.config.globalProperties.$setLocalStorage = setLocalStorage;
app.config.globalProperties.$getLocalStorage = getLocalStorage;
app.config.globalProperties.$removeLocalStorage = removeLocalStorage;
router.beforeEach(function(to, from, next) {
    // 使用getSessionStorage函数正确获取用户信息
    let user = getSessionStorage('user');
    console.log('路由守卫检查用户登录状态:', user);
    
    // 需要登录的页面列表（排除公开页面）
    const publicPages = ['/', '/index', '/businessList', '/businessInfo', '/login', '/register', '/SuperMember'];
    const requiresAuth = !publicPages.includes(to.path);
    
    if (requiresAuth && user == null) {
        console.log('用户未登录，重定向到登录页面');
        next('/login');
        return;
    }
    
    console.log('路由守卫检查通过，继续导航');
    next();
});
app.use(router).mount('#app');
